package com.ebelter.bodyfatscale.ui.activity;


import android.content.Intent;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ebelter.bodyfatscale.R;
import com.ebelter.bodyfatscale.common.Constants;
import com.ebelter.bodyfatscale.model.UserInfo;
import com.ebelter.bodyfatscale.util.UserSpUtil;
import com.ebelter.btlibrary.util.ULog;

import butterknife.BindView;
import butterknife.OnClick;

public class GuestSelectActivity extends BaseActivity {


    public static final int REQUEST_CODE= 147;

    public static final int SELECT_CURRENT_LOGIN_UER= 148;
    public static final int SELECT_GUEST= 149;
    public static final int SELECT_NOCHANGE= 150;
    private static final String TAG = "GuestSelectActivity";


    @BindView(R.id.guestselect_currentlogin_user_ll)
    LinearLayout guestselect_currentlogin_user_ll;


    @BindView(R.id.guest_user_icon_iv)
    ImageView guest_user_icon_iv;

    @BindView(R.id.guest_user_name_tv)
    TextView guest_user_name_tv;

    @BindView(R.id.guestselect_guest_user_ll)
    LinearLayout guestselect_guest_user_ll;


//    @Override
//    protected void onCreate(Bundle savedInstanceState) {
//
//        // Make us non-modal, so that others can receive touch events.
//        getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL, WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
//
//        // ...but notify us that it happened.
//        getWindow().setFlags(WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH, WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH);
//        super.onCreate(savedInstanceState);
//    }

    @Override
    protected int loadLayoutById() {
        return R.layout.guestselect_ly;
    }

    @Override
    protected void initViews() {


        String name = UserSpUtil.readString(Constants.IUser.NICK_NAME);
        int sex = UserSpUtil.readInt(Constants.IUser.SEX);

        if (sex==0){
            guest_user_icon_iv.setImageResource(R.drawable.icon_male_set);
        }else {
            guest_user_icon_iv.setImageResource(R.drawable.icon_female_set);
        }

        guest_user_name_tv.setText(name);

    }


    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (MotionEvent.ACTION_OUTSIDE == event.getAction()) {
            finish();
            return true;
        }
        return super.onTouchEvent(event);
    }

    @Override
    protected void initData() {
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        ULog.i(TAG,"------点击了返回键-------");
        setResult(SELECT_NOCHANGE);
    }

    @OnClick({ R.id.guestselect_currentlogin_user_ll, R.id.guestselect_guest_user_ll})
    public void btClick(View view){
        switch (view.getId()){
            case R.id.guestselect_currentlogin_user_ll:
                ULog.i(TAG,"------选择了当前登录的用户-------");
                setResult(SELECT_CURRENT_LOGIN_UER);
                finish();
                break;
            case R.id.guestselect_guest_user_ll:
                ULog.i(TAG,"------选择了访客用户-------");
                Intent intent=new Intent(this,GuestSelectWriteInfoActivity.class);
                startActivityForResult(intent,1);
                break;


        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode==1){
            ULog.i(TAG,"访客用户选择页面--onActivityResult（）---resultCode = "+resultCode);
            if (resultCode==GuestSelectWriteInfoActivity.SUCCESS_GET_GUEST_USER_INFO){
                ULog.i(TAG,"resultCode = GuestSelectWriteInfoActivity.SUCCESS_GET_GUEST_USER_INFO");
                UserInfo userInfo= (UserInfo) data.getSerializableExtra(GuestSelectWriteInfoActivity.Key);
                ULog.i(TAG,"userInfo =  "+userInfo);
                setResult(SELECT_GUEST,data);
                finish();


            }else if (resultCode==GuestSelectWriteInfoActivity.FAILURE_GET_GUEST_USER_INFO){
                ULog.i(TAG,"resultCode = GuestSelectWriteInfoActivity.FAILURE_GET_GUEST_USER_INFO");
            }




        }



    }
}
